package ExtraDemo.demo01;

public class Foodie implements Runnable {
    Desk desk = null;

    public Foodie(Desk desk) {
        this.desk = desk;
    }

    public Foodie() {
    }

    /**
     * 消费者步骤
     * 1.判断桌子上有没有汉堡包
     * 2.如果没有则等待
     * 3.如果有则开吃
     * 4.吃完之后，汉堡包数量减1，同时唤醒生产者生产
     */
    @Override
    public void run() {
        while (true) {
            synchronized (desk.getLock()) {
                while (desk.getNum() == 0) {
                    try {
                        desk.getLock().wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                int num = desk.getNum();
                desk.setNum(--num);
                System.out.println("吃货在吃汉堡包,现在的个数为" + desk.getNum());
                if (desk.getNum() < 10) {
                    desk.getLock().notifyAll();
                }
            }
        }
    }
}

